package org.example.day;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/09/02 9:54
 */
public class Solution2024 {

    public static void main(String[] args) {
        System.out.println(new Solution2024().maxConsecutiveAnswers3("FFFTTFTTFT", 3));
    }

    String s;
    int n, k;
    public int maxConsecutiveAnswers2(String answerKey, int _k) {
        s = answerKey;
        n = s.length(); k = _k;
        return Math.max(getCnt('T'), getCnt('F'));
    }
    int getCnt(char c) {
        int ans = 0;
        for (int i = 0, j = 0, cnt = 0; i < n; i++) {
            if (s.charAt(i) == c) cnt++;
            while (cnt > k) {
                if (s.charAt(j) == c) cnt--;
                j++;
            }
            ans = Math.max(ans, i - j + 1);
            System.out.println(ans);
        }
        return ans;
    }

    //TF
    public int maxConsecutiveAnswers(String answerKey, int k) {
        int max = 0;
        //前缀合并  TTFF,  2,-2

        List<Integer> mergerList = new ArrayList<>();
        char cur = answerKey.charAt(0);
        int count = 0;
        for (int i = 0; i < answerKey.length(); i++) {
            if(answerKey.charAt(i) == cur){
                if(cur == 'T'){
                    count += 1;
                }else {
                    count -= 1;
                }
            }else {
                mergerList.add(count);
                cur = answerKey.charAt(i);
                if(cur == 'T'){
                    count = 1;
                }else {
                    count = -1;
                }
            }
        }
        mergerList.add(count);

        //假设T开头
        for (int i = 0; i < mergerList.size(); i++) {
            int subCount = 0;
            int subK = k;
            for (int j = i; j < mergerList.size(); j++) {
                if(mergerList.get(j) > 0){
                    subCount += mergerList.get(j);
                }else {
                    if(subK <= 0){
                        break;
                    }
                    int len = -1 * mergerList.get(j);
                    if(subK >= len){
                        subK -= len;
                        subCount += len;
                    }else {
                        subCount+=subK;
                        break;
                    }
                }
                //前补0
                if(subK > 0 && i > 0 && j == mergerList.size()-1){
                    subCount += Math.min(subK, Math.abs(mergerList.get(i-1)));
                }
            }
            System.out.println(subCount);
            max = Math.max(max, subCount);
        }
        //加上F开头
        for (int i = 0; i < mergerList.size(); i++) {
            int subCount = 0;
            int subK = k;
            for (int j = i; j < mergerList.size(); j++) {
                if(mergerList.get(j) < 0){
                    subCount += mergerList.get(j);
                }else {
                    if(subK <= 0){
                        break;
                    }
                    int len = mergerList.get(j);
                    if(subK >= len){
                        subK -= len;
                        subCount -= len;
                    }else {
                        subCount-=subK;
                        break;
                    }
                }
                //前补0
                if(subK > 0 && i > 0 && j == mergerList.size()-1){
                    subCount -= Math.min(subK, Math.abs(mergerList.get(i-1)));
                }
            }
            System.out.println(subCount);
            max = Math.max(max, -1 * subCount);
        }
        return max;
    }

    public int maxConsecutiveAnswers3(String answerKey, int k) {
        return Math.max(getWindowCount('T', answerKey, k), getWindowCount('F', answerKey, k));
    }

    //用c来使用K次替换
    private int getWindowCount(char c, String answerKey, int k) {
        int ans = 0;
        for (int i = 0, j = 0, cnt = 0; i < answerKey.length(); i++) {
            if(answerKey.charAt(i) == c) cnt++;
            while (cnt > k){
                if(answerKey.charAt(j) == c) cnt--;
                j++;
            }
            ans = Math.max(ans, i - j + 1);
        }
        return ans;
    }


}
